package com.example.demo.util;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.util.DigestUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil extends DigestUtils {

    private static final MessageDigest sha256Digest = org.apache.commons.codec.digest.DigestUtils.getSha256Digest();

    private static final char[] DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final char[] DIGITS_UPPER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static String md5DigestAsHex(String filePath) throws IOException {
        return md5DigestAsHex(new File(filePath));
    }

    public static String md5DigestAsHex(File file) throws IOException {
        return DigestUtils.md5DigestAsHex(new FileInputStream(file));
    }

    public static String getStringMd5(String string) {
        return DigestUtils.md5DigestAsHex(string.getBytes());
    }

    public static String getStringMd5(String string, String charsetName) throws UnsupportedEncodingException {
        byte[] bytes = string.getBytes(charsetName);
        return DigestUtils.md5DigestAsHex(bytes);
    }

    public static String getStringMd5(String string, Charset charset) {
        byte[] bytes = string.getBytes(charset);
        return DigestUtils.md5DigestAsHex(bytes);
    }


    public static String SHA256(String string) {
        byte[] cipherBytes = getDigest("SHA-256").digest(string.getBytes());
        char[] chars = encodeHex(cipherBytes, false);
        return new String(chars);
    }

    public static String SHA256(InputStream inputStream) {
        return "";
    }

    public static String SHA256(byte[] bytes) {
        return "";
    }




    public static void main(String[] args) {
        org.apache.commons.codec.digest.DigestUtils.getSha1Digest();
        org.apache.commons.codec.digest.DigestUtils.getSha256Digest();
        org.apache.commons.codec.digest.DigestUtils.getSha384Digest();
        org.apache.commons.codec.digest.DigestUtils.getSha512Digest();
    }



    public static char[] encodeHex(byte[] data) {
        return encodeHex(data, true);
    }

    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    private static char[] encodeHex(byte[] data, char[] toDigits) {
        int l = data.length;
        char[] out = new char[l << 1];
        int i = 0;

        for(int var5 = 0; i < l; ++i) {
            out[var5++] = toDigits[(240 & data[i]) >>> 4];
            out[var5++] = toDigits[15 & data[i]];
        }

        return out;
    }





    public static MessageDigest getDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException var2) {
            throw new IllegalArgumentException(var2);
        }
    }

    public static MessageDigest getMd2Digest() {
        return getDigest("MD2");
    }

    public static MessageDigest getMd5Digest() {
        return getDigest("MD5");
    }

    public static MessageDigest getSha1Digest() {
        return getDigest("SHA-1");
    }

    public static MessageDigest getSha256Digest() {
        return getDigest("SHA-256");
    }

    public static MessageDigest getSha384Digest() {
        return getDigest("SHA-384");
    }

    public static MessageDigest getSha512Digest() {
        return getDigest("SHA-512");
    }
}
